home *** CD-ROM | disk | FTP | other *** search
/ Amiga Developer CD 2.1 / Amiga Developer CD v2.1.iso / NDK / NDK_2.0 / NDK2.0-2 / NewIFF / modules / screen.c < prev    next >
Encoding:
C/C++ Source or Header  |  1991-11-19  |  11.3 KB  |  401 lines

  1. /* screen.c - 2.0 screen module for Display
  2.  * based on scdemo, oscandemo, looki
  3.  * 10/91 - added public screen support by checking user screen tags
  4.  * for SA_PubScreen and setting screen public if found.
  5.  */
  6.  
  7. /*
  8. Copyright (c) 1989, 1990 Commodore-Amiga, Inc.
  9.  
  10. Executables based on this information may be used in software
  11. for Commodore Amiga computers. All other rights reserved.
  12. This information is provided "as is"; no warranties are made.
  13. All use is at your own risk, and no liability or responsibility
  14. is assumed.
  15. */
  16.  
  17. #include "iffp/ilbmapp.h"
  18.  
  19. BOOL   VideoControlTags(struct ColorMap *,ULONG tags, ...);
  20.  
  21. extern struct Library *GfxBase;
  22. extern struct Library *IntuitionBase;
  23.  
  24. struct TextAttr SafeFont = { (UBYTE *) "topaz.font", 8, 0, 0, };
  25. UWORD  penarray[] = {~0};
  26.  
  27. /* default new window if none supplied in ilbm->nw */
  28. struct   NewWindow      defnw = {
  29.    0, 0,                                  /* LeftEdge and TopEdge */
  30.    0, 0,                                /* Width and Height */
  31.    -1, -1,                                /* DetailPen and BlockPen */
  32.    VANILLAKEY|MOUSEBUTTONS,               /* IDCMP Flags with Flags below */
  33.    BACKDROP|BORDERLESS|SMART_REFRESH|NOCAREREFRESH|ACTIVATE|RMBTRAP,
  34.    NULL, NULL,                            /* Gadget and Image pointers */
  35.    NULL,                                  /* Title string */
  36.    NULL,                                  /* Screen ptr null till opened */
  37.    NULL,                                  /* BitMap pointer */
  38.    50, 20,                                /* MinWidth and MinHeight */
  39.    0 , 0,                                 /* MaxWidth and MaxHeight */
  40.    CUSTOMSCREEN                           /* Type of window */
  41.    };
  42.  
  43.  
  44. /* opendisplay - passed ILBMInfo, dimensions, modeID
  45.  *
  46.  *    Attempts to open correct 2.0 modeID screen and window,
  47.  *    else an old 1.3 mode screen and window.
  48.  *
  49.  * Returns *window or NULL.
  50.  */
  51.  
  52. struct Window *opendisplay(struct ILBMInfo *ilbm,
  53.                SHORT wide, SHORT high, SHORT deep,
  54.                ULONG mode)
  55.     {
  56.     struct NewWindow newwin, *nw;
  57.  
  58.     closedisplay(ilbm);
  59.     if(ilbm->scr = openidscreen(ilbm, wide, high, deep, mode))
  60.     {
  61.     nw = &newwin;
  62.     if(ilbm->windef) *nw = *(ilbm->windef);
  63.     else *nw = *(&defnw);
  64.     nw->Screen    = ilbm->scr;
  65.  
  66.     D(bug("sizes: scr= %ld x %ld  passed= %ld x %ld\n",
  67.         ilbm->scr->Width,ilbm->scr->Height,wide,high)); 
  68.  
  69.     nw->Width    = wide;
  70.     nw->Height    = high;
  71.     if (!(ilbm->win = OpenWindow(nw)))
  72.         {
  73.         closedisplay(ilbm);
  74.         D(bug("Failed to open window."));
  75.         }
  76.     else
  77.         {
  78.         if(ilbm->win->Flags & BACKDROP)
  79.         {
  80.         ShowTitle(ilbm->scr, FALSE);
  81.         ilbm->TBState = FALSE;
  82.         }
  83.         }
  84.     }
  85.  
  86.     if(ilbm->scr)    /* nulled out by closedisplay if OpenWindow failed */
  87.     {
  88.     ilbm->vp  = &ilbm->scr->ViewPort;
  89.     ilbm->srp = &ilbm->scr->RastPort;
  90.     ilbm->wrp = ilbm->win->RPort;
  91.     }
  92.     return(ilbm->win);
  93.     }
  94.  
  95.  
  96. /* Under 2.0, returns result of CloseScreen (TRUE for successful close)
  97.  * Under 1.3, always returns TRUE
  98.  */
  99. BOOL closedisplay(struct ILBMInfo *ilbm)
  100.     {
  101.     extern struct Library *IntuitionBase;
  102.     BOOL temp, result = TRUE;
  103.  
  104.     if(ilbm)
  105.     {
  106.         if(ilbm->win)    CloseWindow(ilbm->win), ilbm->win=NULL, ilbm->wrp=NULL;
  107.     if(ilbm->scr)
  108.         {
  109.         if(ilbm->scr->Flags & PUBLICSCREEN)
  110.         {
  111.         PubScreenStatus(ilbm->scr, PSNF_PRIVATE);
  112.         }
  113.         temp = CloseScreen(ilbm->scr);
  114.         if(IntuitionBase->lib_Version >= 36)  result = temp;
  115.         if(result)
  116.         {
  117.         ilbm->scr = NULL;    
  118.             ilbm->vp  = NULL;
  119.             ilbm->srp = NULL;
  120.         }
  121.         }
  122.     }
  123.     return(result);
  124.     }
  125.  
  126.  
  127.  
  128. /* openidscreen - ILBMInfo, dimensions, modeID
  129.  *
  130.  *    Attempts to open correct 2.0 modeID screen with centered
  131.  *    overscan based on user's prefs,
  132.  *    else old 1.3 mode screen.
  133.  *
  134.  * If ilbm->stype includes CUSTOMBITMAP, ilbm->brbitmap will be
  135.  *       used as the screen's bitmap.
  136.  * If ilbm->stags is non-NULL, these tags will be added to the
  137.  *    end of the taglist.
  138.  *
  139.  * Returns *screen or NULL.
  140.  */
  141.  
  142. struct Screen *openidscreen(struct ILBMInfo *ilbm,
  143.                 SHORT wide, SHORT high, SHORT deep,
  144.                 ULONG mode)
  145.     {
  146.     struct NewScreen ns;            /* for old style OpenScreen */
  147.     struct Rectangle spos, dclip, txto, stdo, maxo, uclip;  /* display rectangles */
  148.     struct Rectangle *uclipp;
  149.     struct Screen   *scr = NULL;
  150.     LONG   error, trynew;
  151.     ULONG  bitmaptag, passedtags;
  152.     BOOL   vctl;
  153.  
  154.     if (trynew = ((((struct Library *)GfxBase)->lib_Version >= 36)&&
  155.           (((struct Library *)IntuitionBase)->lib_Version >= 36)))
  156.     {
  157.         /* if >= v36, see if mode is available */
  158.     if(error = ModeNotAvailable(mode))
  159.         {
  160.         D(bug("Mode $%08lx not available, error=%ld:\n",mode,error));
  161.         /* if not available, try fall back mode */
  162.         mode = modefallback(mode,wide,high,deep);
  163.         error = ModeNotAvailable(mode);
  164.  
  165.         D(bug("$%08lx ModeNotAvailable=%ld:\n",mode,error));
  166.         }
  167.  
  168.     if(error) trynew = FALSE;
  169.     else trynew=((QueryOverscan(mode,&txto,OSCAN_TEXT))&&
  170.             (QueryOverscan(mode,&stdo,OSCAN_STANDARD))&&
  171.                 (QueryOverscan(mode,&maxo,OSCAN_MAX)));
  172.     }
  173.  
  174.     D(bug("\nILBM: w=%ld, h=%ld, d=%ld, mode=0x%08lx\n",
  175.         wide,high,deep,mode));    
  176.     D(bug("OPEN: %s.\n",
  177.         trynew    ? "Is >= 2.0 and mode available, trying OpenScreenTags"
  178.         : "Not 2.0, doing old OpenScreen"));
  179.  
  180.     if(trynew)
  181.     {
  182.     /* If user clip type specified and available, use it */
  183.     if(ilbm->Video) ilbm->ucliptype = OSCAN_VIDEO;
  184.     if((ilbm->ucliptype)&&(QueryOverscan(mode,&uclip,ilbm->ucliptype)))
  185.         uclipp = &uclip;
  186.     else uclipp = NULL;
  187.  
  188.     clipit(wide,high,&spos,&dclip,&txto,&stdo,&maxo,uclipp);
  189.  
  190.     D(bug("Using dclip  %ld,%ld  to  %ld,%ld... width=%ld height=%ld\n",
  191.             dclip.MinX,dclip.MinY,dclip.MaxX,dclip.MaxY,
  192.             dclip.MaxX-dclip.MinX+1,dclip.MaxY-dclip.MinY+1));
  193.     D(bug("spos->minx = %ld, spos->miny = %ld\n",spos.MinX,spos.MinY));
  194.     D(bug("DEBUG: About to attempt OpenScreenTags\n"));
  195.  
  196.     bitmaptag = ((ilbm->brbitmap)&&(ilbm->stype & CUSTOMBITMAP)) ?
  197.         SA_BitMap : TAG_IGNORE;
  198.     passedtags = ilbm->stags ? TAG_MORE : TAG_IGNORE;
  199.  
  200.     scr=(struct Screen *)OpenScreenTags((struct NewScreen *)NULL,
  201.         SA_DisplayID,    mode,
  202.         SA_Type,    ilbm->stype,
  203.         SA_Behind,    TRUE,
  204.         SA_Top,        spos.MinY,
  205.         SA_Left,    spos.MinX,
  206.         SA_Width,    wide,
  207.         SA_Height,    high,
  208.         SA_Depth,    deep,
  209.         SA_DClip,    &dclip,
  210.         SA_AutoScroll,    ilbm->Autoscroll ? TRUE : FALSE,
  211.         SA_Title,    ilbm->stitle,
  212.         SA_Font,    &SafeFont,
  213.         SA_Pens,    penarray,
  214.         SA_ErrorCode,    &error,
  215.         bitmaptag,    ilbm->brbitmap,
  216.         passedtags,    ilbm->stags,
  217.         TAG_DONE
  218.         );
  219.  
  220.         D(bug("DEBUG: OpenScreenTags scr at 0x%lx\n",scr));
  221.  
  222.         if(scr)
  223.         {
  224.         /* If caller specified a public screen, open for business */
  225.         if(scr->Flags & PUBLICSCREEN)
  226.             {
  227.             PubScreenStatus(scr,NULL);
  228.             }
  229.         if(ilbm->Notransb)
  230.             {
  231.             vctl=VideoControlTags(scr->ViewPort.ColorMap,
  232.                 VTAG_BORDERNOTRANS_SET, TRUE,
  233.                 TAG_DONE);
  234.  
  235.     D(bug("VideoControl to set bordernotrans, error = %ld\n",vctl));
  236.  
  237.             MakeScreen(scr);
  238.             RethinkDisplay();
  239.             }
  240.         }
  241.         else message("%s\n",openScreenErr(error));
  242.         }
  243.  
  244.     if(!scr)
  245.     {
  246.     /* ns initialization for 1.3 old style OpenScreen only
  247.          */
  248.     ns.LeftEdge = ns.TopEdge = 0;   
  249.     ns.Width     =    wide;
  250.     ns.Height     =    high;
  251.     ns.Depth    =    deep;
  252.     ns.ViewModes    =     modefallback(mode,wide,high,deep);
  253.     ns.DetailPen    =    0;
  254.     ns.BlockPen    =    1;
  255.     ns.Gadgets    =    NULL;
  256.     ns.CustomBitMap    =    ((ilbm->brbitmap)&&(ilbm->stype & CUSTOMBITMAP))
  257.                     ? ilbm->brbitmap : NULL;
  258.     ns.Font        =    &SafeFont;
  259.     ns.DefaultTitle =     ilbm->stitle;
  260.     ns.Type        =    ilbm->stype & 0x01FF;  /* allow only 1.3 types */
  261.  
  262.     scr=(struct Screen *)OpenScreen(&ns);
  263.  
  264.     D(bug("DEBUG: ns.ViewModes=0x%lx, vp->Modes=0x%lx\n",
  265.                  ns.ViewModes,scr->ViewPort.Modes));
  266.     D(bug("DEBUG: non-extended scr at 0x%lx (0=failure)\n",scr));
  267.     }
  268.     return(scr);
  269.     }
  270.  
  271.  
  272. /*
  273.  * modefallback - passed a mode id, attempts to provide a
  274.  *                suitable old mode to use instead
  275.  */
  276.  
  277. /* for old 1.3 screens */
  278. #define MODE_ID_MASK (LACE|HIRES|HAM|EXTRA_HALFBRITE)
  279.  
  280. ULONG modefallback(ULONG mode, SHORT wide, SHORT high, SHORT deep)
  281. {
  282. ULONG newmode;
  283.  
  284.     /* For now, simply masks out everything but old mode bits.
  285.      * This is just a cheap way to get some kind of display open
  286.      *   and may be totally invalid for future modes.
  287.      * Should search the display database for a suitable mode
  288.      * based on the specific needs of your application.
  289.      */
  290.     newmode = mode & MODE_ID_MASK;
  291.  
  292.     D(bug("Try 0x%08lx instead of 0x%08lx\n",newmode,mode));
  293.     return(newmode);
  294. }
  295.  
  296.  
  297. /*
  298.  * clipit - passed width and height of a display, and the text, std, and
  299.  *          max overscan rectangles for the mode, clipit fills in the
  300.  *          spos (screen pos) and dclip rectangles to use in centering.
  301.  *          Centered around smallest containing user-editable oscan pref,
  302.  *          with dclip confined to legal maxoscan limits.
  303.  *          Screens which center such that their top is below text
  304.  *          oscan top, will be moved up.
  305.  *          If a non-null uclip is passed, that clip is used instead.
  306.  */
  307. void clipit(SHORT wide, SHORT high,
  308.         struct Rectangle *spos, struct Rectangle *dclip,
  309.         struct Rectangle *txto, struct Rectangle *stdo,
  310.         struct Rectangle *maxo, struct Rectangle *uclip)
  311. {
  312. struct  Rectangle *besto;
  313. SHORT    minx, maxx, miny, maxy;
  314. SHORT    txtw, txth, stdw, stdh, maxw, maxh, bestw, besth;
  315.  
  316.     /* get the txt, std and max widths and heights */
  317.     txtw = txto->MaxX - txto->MinX + 1;
  318.     txth = txto->MaxY - txto->MinY + 1;
  319.     stdw = stdo->MaxX - stdo->MinX + 1;
  320.     stdh = stdo->MaxY - stdo->MinY + 1;
  321.     maxw = maxo->MaxX - maxo->MinX + 1;
  322.     maxh = maxo->MaxY - maxo->MinY + 1;
  323.  
  324.     if((wide <= txtw)&&(high <= txth))
  325.     {
  326.     besto = txto;
  327.     bestw = txtw;
  328.     besth = txth;
  329.  
  330.     D(bug("Best clip is txto\n"));
  331.     }
  332.     else
  333.     {
  334.     besto = stdo;
  335.     bestw = stdw;
  336.     besth = stdh;
  337.  
  338.     D(bug("Best clip is stdo\n"));
  339.     }
  340.  
  341.     D(bug("TXTO: mnx=%ld mny=%ld mxx=%ld mxy=%ld  stdw=%ld stdh=%ld\n",
  342.         txto->MinX,txto->MinY,txto->MaxX,txto->MaxY,txtw,txth));
  343.     D(bug("STDO: mnx=%ld mny=%ld mxx=%ld mxy=%ld  stdw=%ld stdh=%ld\n",
  344.         stdo->MinX,stdo->MinY,stdo->MaxX,stdo->MaxY,stdw,stdh));
  345.     D(bug("MAXO: mnx=%ld mny=%ld mxx=%ld mxy=%ld  maxw=%ld maxh=%ld\n",
  346.         maxo->MinX,maxo->MinY,maxo->MaxX,maxo->MaxY,maxw,maxh));
  347.  
  348.     if(uclip)
  349.     {
  350.     *dclip = *uclip;
  351.         spos->MinX = uclip->MinX;
  352.     spos->MinY = uclip->MinY;
  353.  
  354.     D(bug("UCLIP: mnx=%ld mny=%ld maxx=%ld maxy=%ld\n",
  355.             dclip->MinX,dclip->MinY,dclip->MaxX,dclip->MaxY));
  356.     }
  357.     else
  358.     {
  359.     /* CENTER the screen based on best oscan prefs
  360.      * but confine dclip within max oscan limits
  361.      *
  362.      * FIX MinX first */
  363.     spos->MinX = minx = besto->MinX - ((wide - bestw) >> 1);
  364.     maxx = wide + minx - 1;
  365.     if(maxx > maxo->MaxX)  maxx = maxo->MaxX;    /* too right */
  366.     if(minx < maxo->MinX)  minx = maxo->MinX;    /* too left  */
  367.  
  368.     D(bug("DCLIP: minx adjust from %ld to %ld\n",spos->MinX,minx));
  369.  
  370.     /* FIX MinY */
  371.     spos->MinY = miny = besto->MinY - ((high - besth) >> 1);
  372.     /* if lower than top of txto, move up */
  373.     spos->MinY = miny = MIN(spos->MinY,txto->MinY);
  374.     maxy = high + miny - 1;
  375.     if(maxy > maxo->MaxY)  maxy = maxo->MaxY;    /* too down  */
  376.     if(miny < maxo->MinY)  miny = maxo->MinY;    /* too up    */
  377.  
  378.     D(bug("DCLIP: miny adjust from %ld to %ld\n",spos->MinY,miny));
  379.  
  380.     /* SET up dclip */
  381.     dclip->MinX = minx;
  382.     dclip->MinY = miny;
  383.     dclip->MaxX = maxx;
  384.     dclip->MaxY = maxy;
  385.  
  386.     D(bug("CENTER: mnx=%ld mny=%ld maxx=%ld maxy=%ld\n",
  387.             dclip->MinX,dclip->MinY,dclip->MaxX,dclip->MaxY));
  388.     }
  389. }
  390.  
  391. /*----------------------------------------------------------------------*/
  392.  
  393. BOOL VideoControlTags(struct ColorMap *cm, ULONG tags, ...)
  394.     {
  395.     return (VideoControl(cm, (struct TagItem *)&tags));
  396.     }
  397.  
  398.  
  399. /*----------------------------------------------------------------------*/
  400.  
  401.